package com.adcc.swingui.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.adcc.swingui.bean.MenuBean;
import com.adcc.swingui.bean.MenuComponentBean;
import com.adcc.swingui.bean.PanelComponentBean;
import com.adcc.swingui.bean.StatusBean;
import com.adcc.swingui.bean.StatusComponentBean;
/**
 * 状态栏配置
 * @author charlie
 *
 */
public class StatusBarConf {
	private static Logger logger = Logger.getLogger(StatusBarConf.class);
	private static Map<String, StatusBean> statusConf =  new HashMap<String, StatusBean>();
	public static void initConf(String path) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				StatusBean bean =  new StatusBean();
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					Element statusBar = (Element) i.next();
					List<StatusComponentBean> componentList = new ArrayList<StatusComponentBean>();
					if (statusBar.getName().equals("statusBar")) {
						bean.setName(statusBar.attributeValue("name"));
						for (Iterator j = statusBar.elementIterator(); j.hasNext();) {
							Element component = (Element) j.next();
							if (component.getName().equals("component")) {
								StatusComponentBean componentBean = new StatusComponentBean();
								componentBean.setName(component.attributeValue("name"));
								componentBean.setClassName(component.attributeValue("className"));
								componentList.add(componentBean);
							}
						}
						bean.setStatusList(componentList);
					}
					statusConf.put(bean.getName(), bean);
				}
			}
		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}
	
	public static void main(String[] args) {
		initConf("conf/default/status.xml");
		Set<String> keySet = statusConf.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String key = it.next();
			StatusBean value = statusConf.get(key);
			System.out.println("name:" + value.getName());
			System.out.println("i18nName" + value.getI18nName());
			List<StatusComponentBean> a  = new ArrayList<StatusComponentBean>();
			a = value.getStatusList();
			if (a != null && a.size() != 0) {

				for (int i = 0; i < a.size(); i++) {
					StatusComponentBean comp = new StatusComponentBean();
					comp = a.get(i);
					System.out.println("componentName:" + comp.getName());
					System.out.println("componentClassName:" + comp.getClassName());
				}
			}
		}
	}

	public static Map<String, StatusBean> getStatusConf() {
		return statusConf;
	}
	
	
}
